package eazy.StackAndQueue;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class isValid_20 {
    public static void main(String[] args) {

    }

    /**
     *先获取字符串长度，判断是否为奇
     * 用map存储括号
     * 创建一个栈
     * 循环字符串
     */
    public boolean isValid(String s) {
        int n = s.length();
        if (n % 2 == 1) {
            return false;
        }
        Map<Character, Character> paris = new HashMap<Character, Character>() {
            {
                    put(')', '(');
                    put(']', '[');
                    put('}', '{');
            }
        };
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (paris.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != paris.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
}
